<template>
  <div>
<!--    查询条件-->
    <div style="margin-bottom: 5px;">
      <el-input v-model="bookname" placeholder="请输入书名" suffix-icon="el-icon-search" style="width: 200px;"
                @keyup.enter.native="loadPost"></el-input>
      <el-select v-model="bookuid" placeholder="请选择类目" style="margin-left: 5px;">
        <el-option
            v-for="item in typeData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>

      <el-button style="margin-left: 5px;" type="primary" @click="loadPost">查询</el-button>
      <el-button style="margin-left: 5px;" type="success" @click="resetParam">重置</el-button>

      <el-button style="margin-left: 5px;" type="primary" @click="add">新增</el-button>


    </div>
    <el-table :data="tableData"
              :header-cell-style="{ background:'#f2f5fc',color:'#555555' }"
              border
    >
      <el-table-column prop="id" label="id" width="60">
      </el-table-column>
      <el-table-column prop="bookname" label="书名" width="120">
      </el-table-column>
      <el-table-column prop="bookcode" label="图书编码" width="120">
      </el-table-column>
      <el-table-column prop="bookuid" label="图书类目" width="120" :formatter="formatBookuid" >
      </el-table-column>
      <el-table-column prop="writer" label="作者" width="120">
      </el-table-column>
      <el-table-column prop="press" label="出版社" width="150">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="120">
      </el-table-column>

      <el-table-column  prop="intro" label="图书简介" width="500">
      </el-table-column>
<!--      <el-table-column prop="isadmin" label="isadmin" width="120">
&lt;!&ndash;        <template slot-scope="scope">
          <el-tag
              :type="scope.row.isadmin === '1' ? 'primary' : 'success'"
              disable-transitions>{{scope.row.isadmin === '1' ?  '管理员' : '用户'}}</el-tag>
        </template>&ndash;&gt;
      </el-table-column>-->
      <el-table-column prop="operate" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="确定删除?"
              @confirm="del(scope.row.id)"
              style="margin-left: 5px;"
          >
            <el-button slot="reference" size="mini" type="danger" >删除</el-button>
          </el-popconfirm>
        </template>
        <!--      <template slot-scope="scope">-->
        <!--        <el-button-->
        <!--            size="mini"-->
        <!--            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
        <!--        <el-button-->
        <!--            size="mini"-->
        <!--            type="danger"-->
        <!--            @click="handleDelete(scope.$index, scope.row)">删除</el-button>-->
        <!--      </template>-->
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        :title="this.flage ==='modflage'?'编辑':'新增' "
        :visible.sync="centerDialogVisible"
        width="30%"
        center>

<!--      新增和编辑-->
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="书名" prop="bookname">
          <el-col :span="15">
            <el-input v-model="form.bookname"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="图书编码" prop="bookcode">
          <el-col :span="15">
            <el-input v-model="form.bookcode"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="图书类目" prop="bookuid">
          <el-col :span="15">
            <el-select v-model="form.bookuid" placeholder="请选择类目" style="margin-left: 5px;">
              <el-option
                  v-for="item in typeData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="作者" prop="writer">
          <el-col :span="15">
            <el-input v-model="form.writer"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="出版社" prop="press">
          <el-col :span="15">
            <el-input v-model="form.press"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-col :span="15">
            <el-input v-model="form.price" ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="图书简介" prop="intro">
          <el-col :span="15">
            <el-input v-model="form.intro"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "BookManage",
  data() {

    flage:''




    return {
      typeData:[],
      tableData:[],
      pageSize:10,
      pageNum:1,
      total:0,
      bookname:'',
      bookuid:'',



      centerDialogVisible:false,
      form:{
        id:'',
        bookname:'',
        bookcode:'',
        writer:'',
        press:'',
        price:'',
        bookuid:'',
        intro:''
      },
      rules: {
        bookname: [
          { required: true, message: '请输入书名', trigger: 'blur' },
        ],
        bookuid: [
          { required: true, message: '请输入图书类目', trigger: 'blur' },
        ],
        bookcode: [
          { required: true, message: '请输入图书编码', trigger: 'blur' },
        ]

      }
    }
  },
  methods:{
    //将类目表里的类目名称传到图书信息中
    formatBookuid(row){
      let temp = this.typeData.find(item=>{
        return item.id == row.bookuid
      })
      //获取类目名称
      return temp && temp.name
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
    //修改信息
    doMod(){
      this.$axios.post(this.$httpUrl+'/books/update',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){

          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible = false
          this.loadPost()
          this.resetForm()
        }else {
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    //新增信息
    doSave(){
      this.$axios.post(this.$httpUrl+'/books/save',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible = false
          this.loadPost()
          this.resetForm()
        }else {
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    //判断方法
    save(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.flage === 'modflage'){
            this.doMod();
          }else{
            this.doSave();
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //删除
    del(id){
      console.log(id)

      this.$axios.get(this.$httpUrl+'/books/del?id='+id).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){

          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
      })


    },
    //修改
    mod(row){
      this.flage= 'modflage'
      console.log(row)

      this.centerDialogVisible = true
      this.$nextTick(()=>{
        //编辑功能赋值到表单
        this.form.id = row.id
        this.form.bookname = row.bookname
        this.form.bookcode = row.bookcode
        this.form.writer = row.writer
        this.form.press =row.press
        this.form.price = row.price
        this.form.bookuid = row.bookuid
        this.form.intro = row.intro
      })

    },
    //添加
    add(){
      this.flage = 'addflage'
      this.centerDialogVisible = true
      this.$nextTick(()=>{
        this.resetForm()
      })



    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },

    resetParam(){
      this.bookname=''
      this.bookuid=''

    },
    loadPost(){
      this.$axios.post(this.$httpUrl+'/books/listPage',{
        pageSize:this.pageSize,
        pageNum:this.pageNum,
        param:{
          bookname:this.bookname,
          bookuid:this.bookuid+'',


        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else ('获取数据失败')

      })
    },
    //引入类目表中的数据
    loadType(){
      this.$axios.get(this.$httpUrl+'/type/list').then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.typeData=res.data
        }else ('获取数据失败')

      })
    }
  },
  beforeMount() {
    this.loadType()
    this.loadPost()
  }
}
</script>

<style scoped>

</style>